package com.woniuxy.controller;

import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import com.woniuxy.util.Result;
import com.woniuxy.util.SmsUtils;
import com.woniuxy.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Random;

@RestController
@CrossOrigin
public class UserController {

    int i = 2;
    @Autowired
    private UserService userService;
    @GetMapping("user/selectUser")
    public Map<String,Object> selectUser(Integer userId){
//        根据userId查询营地先关信息
        Map<String,Object> map=userService.selectUserNowCampOrCity(userId);
        return map;
    }
    //登录
    @RequestMapping("/user/login")
    public Result login(String userName, String password, HttpSession session) throws Exception{
        UserVO user = userService.login(userName, password);
        System.out.println("userVo======="+user);
        if (user!=null){
            session.setAttribute("user",user);
            return new Result("200","success",user,null);
        }
        return new Result("201","success",user,null);
    }
    //注册
    @RequestMapping("/user/register")
    public Result register(User user) throws Exception{
        user.setAccount(0.0);
        user.setImgUrl("http://www.joyrv.cn/data/upload/5ecf42db77277_s.jpg");
        user.setStatus(1);
        userService.insertUser(user);
        return new Result("200","success",user,null);
    }
    //判断用户电话是否存在
    @RequestMapping("/user/checkTel")
    public Result checkTel(String tel) throws Exception{
        if (userService.selectTel(tel)!=null){
            return new Result("200","success",null,null);
        }
        return new Result("201","success", null,null);
    }
    //短信验证
    @RequestMapping("/user/sendSms")
    public Result sentSms(String tel) throws Exception{
        String code = Integer.toString(SmsUtils.newcode());
        SmsUtils.sendSms(tel,code);
        return new Result("200",code, null,null);
    }

//    @RequestMapping("/user/selectUser")
//    public Result selectUser(Integer userId) throws Exception{
//        return new Result("200","success", userService.selectUserByUserId(userId),null);
//    }

    //用户更新头像
    @RequestMapping("/updateUserImgUrl")
    public Result updateUserImgUrl(User user){
        userService.updateUserImgUrl(user);
        return new Result("200","success", null,null);
    }
}
